<?php
/**
 * Author: liushanshan
 * Date: 2019/2/15 0015
 */
namespace app\admin\model;
use app\admin\validate\MemberVal;
use app\admin\validate\ValidateFun;
use think\Db;
use think\image\Exception;
use think\Model;
use think\Request;

class Member extends Model{
    //登录
    public function login(){
//       try{
           #获取数据
           $data = Request::instance()->param();

           #验证参数
           $vali = MemberVal::login();
           $validate = new ValidateFun($vali['rule'],$vali['msg']);
           if (!$validate->check($data)){
               return ['code'=>$validate->getError(),'msg'=>config('msg.'.$validate->getError())];
           }

           #查找数据
           $info = Db::name('member')->where(['username'=>$data['username'],'del'=>1])->find();
           if ($info && $info['password']==getMd5($data['password'],$data['username'],$info['code'])){
               if($data['radio']){ // 记住账号
                   cookie('username',$data['username'],7*24*3600);
                   cookie('password',$data['password'],7*24*3600);
                   cookie('id', $info['id'], 7*24*3600);
                   cookie('rules',$info['identity'], 7*24*3600);
               }
               if(empty($data['radio'])){
                   cookie(null);
               }
               cookie('username',$data['username'],7*24*3600);
               session('id',$info['id']);
               session('rules',$info['identity']);
               session('username',$data['username']);

               return ['code'=>200,'msg'=>config('msg.200')];
           }
           return ['code'=>10004,'msg'=>config('msg.10004')];#账号或密码错误

//       }catch (\Exception $e){
//           return ['code'=>201,'msg'=>config('msg.201')];
//       }
    }
}